<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AlertTableOrderAddIsInvoice extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $orderFunc = function (Blueprint $table) {
            $table->unsignedTinyInteger('invoiceStatus')
                  ->default(0)
                  ->after('refundStatus')
                  ->comment('开票状态 0未开票 1等待开票(开票中) 2已开票 3 取消中 4 已取消  5 已取消(红冲)');

            $table->unsignedTinyInteger('invoiceCanCancelCount')
                  ->default(1)
                  ->after('refundStatus')
                  ->comment('能取消开票次数 0 不能取消');
        };

        $waimaiFunc = function (Blueprint $table) {
            $table->unsignedTinyInteger('invoiceStatus')
                  ->default(0)
                  ->after('rejectReason')
                  ->comment('开票状态 0未开票 1等待开票(开票中) 2已开票 3 取消中 4 已取消  5 已取消(红冲)');

            $table->unsignedTinyInteger('invoiceCanCancelCount')
                  ->default(1)
                  ->after('rejectReason')
                  ->comment('能取消开票次数 0 不能取消');
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->table('order' . $this->name, $orderFunc);
            Schema::connection("mysqlWaiMai")->table('waimai_orders' . $this->name, $waimaiFunc);
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        for ($i = 0; $i <= 99; $i++) {
            $name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);

            Schema::connection("mysqlOrder")->table('order' . $name, function (Blueprint $table) {
                $table->dropColumn('invoiceStatus');
                $table->dropColumn('invoiceCanCancelCount');
            });

            Schema::connection("mysqlWaiMai")->table('waimai_orders' . $name, function (Blueprint $table) {
                $table->dropColumn('invoiceStatus');
                $table->dropColumn('invoiceCanCancelCount');
            });
        }
//        die;
    }
}
